package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.bo.adminbo.AdminListBo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketFootprint;
import com.cskaoyan.app.bean.po.MarketFootprintExample;
import com.cskaoyan.app.bean.po.MarketGoods;
import com.cskaoyan.app.bean.po.MarketGoodsExample;
import com.cskaoyan.app.bean.vo.wx.WxFootPrintListVo;
import com.cskaoyan.app.mapper.MarketFootprintMapper;
import com.cskaoyan.app.mapper.MarketGoodsMapper;
import com.cskaoyan.app.wxservice.WxFootPrintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kiggy
 * @since 2023/01/01 15:22
 */
@Service
public class WxFootPrintServiceImpl implements WxFootPrintService {

    @Autowired
    MarketFootprintMapper footprintMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     * 内容包括：brief,picUrl,addTime,goodsId,name,id,retailPrice
     * 获得所有footprint，通过footprint的goodsId获得参数picUrl，brief等，将值赋给WxFootprintVo
     * @param listBo
     * @return
     */
    @Override
    @Transactional
    public CommonData listFootPrint(AdminListBo listBo){
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        footprintExample.setOrderByClause("update_time desc");
        List<MarketFootprint> footprints = footprintMapper.selectByExample(footprintExample);
        if (footprints.size()==0){
            return null;
        }
        List<WxFootPrintListVo> footPrintListVos = new ArrayList<>();
        Iterator<MarketFootprint> iterator = footprints.iterator();
        while (iterator.hasNext()){
            WxFootPrintListVo footPrintListVo = new WxFootPrintListVo();
            MarketFootprint footprint = iterator.next();
            MarketGoods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            footPrintListVo.setAddTime(footprint.getUpdateTime());
            footPrintListVo.setBrief(goods.getBrief());
            footPrintListVo.setGoodsId(goods.getId());
            footPrintListVo.setId(footprint.getId());
            footPrintListVo.setName(goods.getName());
            footPrintListVo.setPicUrl(goods.getPicUrl());
            footPrintListVo.setRetailPrice(goods.getRetailPrice());
            footPrintListVos.add(footPrintListVo);
        }
        PageInfo<WxFootPrintListVo> pageInfo = new PageInfo<>(footPrintListVos);
        return CommonData.data(pageInfo);
    }

    @Override
    public int deleteFootPrint(Integer id) {
        MarketFootprint marketFootprint = new MarketFootprint();
        marketFootprint.setDeleted(true);
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        footprintMapper.updateByExampleSelective(marketFootprint,footprintExample);
        return 0;
    }

}
